﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Globalization;
namespace PrintDrawing.BaseClassDraw
{
    /// <summary>
    /// Một node Element đại điện cho cấu hình của 1 textBox Gồm có
    /// - Tọa độ
    /// - Bingdinh or
    /// - Dữ liệu hiện tại
    /// - chiều dài
    /// - chiều cao
    /// - font chữ, kiểu chữ, (- màu chữ có thể bổ xung sau)
    /// </summary>
    internal class NodeElement
    {
        //for <ELEMENT/> possition
        private const string PAGE_POSSITION_X = "X";
        private const string PAGE_POSSITION_Y = "Y";
        //for <ELEMENT/> binding
        private const string PAGE_BINDING_ISVALUE = "ISVALUE";
        private const string PAGE_BINDING_TABLENAME = "TableName";
        private const string PAGE_BINDING_COLUM = "Colum";
        private const string PAGE_BINDING_VALUE = "Value";
        //for <ELEMENT/> style : font, height, width, type
        private const string STYLE_FONT = "Font";
        private const string STYLE_SIZE = "Size";
        private const string STYLE_HEIGHT = "Height";
        private const string STYLE_WIDTH = "Width";
        private const string STYLE_TYPE = "Type";
        private const string STYLE_TYPEOFCONTROL = "TypeOfControl";


        private const string NODE_ELEMENT_NAME = "NodeElement";
        private const string NODE_POSSITION_NAME = "Possition";
        private const string NODE_STYLE_NAME = "Style";
        private const string NODE_BINDING_NAME = "Binding";
        //dữ liệu tọa độ
        private double _x = 0; // tọa độ X

        public double X
        {
            get { return _x; }
            set { _x = value; }
        }
        private double _y = 0; // tọa độ y

        public double Y
        {
            get { return _y; }
            set { _y = value; }
        }

        // dữ liệu binding
        private string _nameTable = "";

        public string NameTable
        {
            get { return _nameTable; }
            set { _nameTable = value; }
        }
        private string _nameColum = "";

        public string NameColumn
        {
            get { return _nameColum; }
            set { _nameColum = value; }
        }

        //Dữ liệu value nếu có
        private string _value = "";

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        //dữ liệu style
        private string _font = "";

        public string Font
        {
            get { return _font; }
            set { _font = value; }
        }
        private double _size = 10;

        public double Size
        {
            get { return _size; }
            set { _size = value; }
        }
        private string _styleFont = "";

        public string StyleFont
        {
            get { return _styleFont; }
            set { _styleFont = value; }
        }

        private string _typeOfControl = "";

        public string TypeOfControl
        {
            get { return _typeOfControl; }
            set { _typeOfControl = value; }
        }

        private double _height = 25;

        public double Height
        {
            get { return _height; }
            set { _height = value; }
        }
        private double _with = 20;

        public double With
        {
            get { return _with; }
            set { _with = value; }
        }

        private int _isValue = 0;// ko co value
        public int Is_Value
        {
            get { return _isValue; }
            set { _isValue = value; }
        }

        public XmlNode ExportElementToXmlNode(XmlDocument doc)
        {
            //element
            XmlNode elementNode = doc.CreateElement(NODE_ELEMENT_NAME);
            //possition
            XmlNode possitionNode = doc.CreateElement(NODE_POSSITION_NAME);
            //element possion X & Y
            XmlAttribute possitionAttribute = doc.CreateAttribute(PAGE_POSSITION_X);
            possitionAttribute.Value = this.X.ToString(CultureInfo.GetCultureInfo("en-us"));
            possitionNode.Attributes.Append(possitionAttribute);

            possitionAttribute = doc.CreateAttribute(PAGE_POSSITION_Y);
            possitionAttribute.Value = this.Y.ToString(CultureInfo.GetCultureInfo("en-us"));
            possitionNode.Attributes.Append(possitionAttribute);
            elementNode.AppendChild(possitionNode);


            //element binding
            XmlNode bindingNode = doc.CreateElement(NODE_BINDING_NAME);
            //attrtibute table name
            XmlAttribute bindingAttribute = doc.CreateAttribute(PAGE_BINDING_TABLENAME);
            bindingAttribute.Value = this.NameTable;
            bindingNode.Attributes.Append(bindingAttribute);
            // attribute table colum
            bindingAttribute = doc.CreateAttribute(PAGE_BINDING_COLUM);
            bindingAttribute.Value = this.NameColumn;
            bindingNode.Attributes.Append(bindingAttribute);
            // attribute value
            bindingAttribute = doc.CreateAttribute(PAGE_BINDING_VALUE);
            bindingAttribute.Value = this.Value;
            bindingNode.Attributes.Append(bindingAttribute);
            elementNode.AppendChild(bindingNode);
            // attribute iSvalue
            bindingAttribute = doc.CreateAttribute(PAGE_BINDING_ISVALUE);
            bindingAttribute.Value = this.Is_Value.ToString();
            bindingNode.Attributes.Append(bindingAttribute);
            elementNode.AppendChild(bindingNode);                       

            //element style
            XmlNode styleNode = doc.CreateElement(NODE_STYLE_NAME);
            //attrtibute font
            XmlAttribute styleAttribute = doc.CreateAttribute(STYLE_FONT);
            styleAttribute.Value = this.Font;
            styleNode.Attributes.Append(styleAttribute);
            // attribute height
            styleAttribute = doc.CreateAttribute(STYLE_HEIGHT);
            styleAttribute.Value = this.Height.ToString();
            styleNode.Attributes.Append(styleAttribute);
            // attribute width
            styleAttribute = doc.CreateAttribute(STYLE_WIDTH);
            styleAttribute.Value = this.With.ToString();
            styleNode.Attributes.Append(styleAttribute);
            // attribute size
            styleAttribute = doc.CreateAttribute(STYLE_SIZE);
            styleAttribute.Value = this.Size.ToString();
            styleNode.Attributes.Append(styleAttribute);
            // attribute type
            styleAttribute = doc.CreateAttribute(STYLE_TYPE);
            styleAttribute.Value = this.StyleFont;
            styleNode.Attributes.Append(styleAttribute);
            elementNode.AppendChild(styleNode);
            // attribute typeofcontrol
            styleAttribute = doc.CreateAttribute(STYLE_TYPEOFCONTROL);
            styleAttribute.Value = this.TypeOfControl;
            styleNode.Attributes.Append(styleAttribute);
            elementNode.AppendChild(styleNode);

            return elementNode;
        }

        public static List<NodeElement> ExportXmlToClass(XmlNode root)
        {
            //get root=<page>
            List<NodeElement> nodeElements = new List<NodeElement>();
            ///search các node page          
            foreach (XmlNode nodeElementXML in root.ChildNodes)
            {
                //1 one is <NodeElement> serch
                if (nodeElementXML.Name != NODE_ELEMENT_NAME)
                {
                    continue;
                }
                NodeElement nodeElement = new NodeElement();
                foreach (XmlNode node in nodeElementXML.ChildNodes)
                {                   
                    //d is possition
                    if (node.Name == NODE_POSSITION_NAME) //attribute is possition
                    {
                        /// search with attribute
                        foreach (XmlAttribute a in node.Attributes)
                        {
                            if (a.Name == PAGE_POSSITION_X)
                                nodeElement.X = double.Parse(a.Value, CultureInfo.GetCultureInfo("en-us"));
                            if (a.Name == PAGE_POSSITION_Y)
                                nodeElement.Y = double.Parse(a.Value, CultureInfo.GetCultureInfo("en-us"));
                        }
                    }
                    else if (node.Name == NODE_BINDING_NAME) //atrribute is binding
                    {
                        /// search with attribute
                        foreach (XmlAttribute a in node.Attributes)
                        {
                            if (a.Name == PAGE_BINDING_TABLENAME)
                                nodeElement.NameTable = a.Value;
                            if (a.Name == PAGE_BINDING_COLUM)
                                nodeElement.NameColumn = a.Value;
                            if (a.Name == PAGE_BINDING_VALUE)                            
                                nodeElement.Value = a.Value;
                            if (a.Name == PAGE_BINDING_ISVALUE)
                                nodeElement.Is_Value = int.Parse(a.Value);
                        }
                    }
                    else if (node.Name == NODE_STYLE_NAME) //attribute is style
                    {
                        /// search with attribute
                        foreach (XmlAttribute a in node.Attributes)
                        {
                            if (a.Name == STYLE_FONT)
                                nodeElement.Font = a.Value;
                            if (a.Name == STYLE_HEIGHT)
                                nodeElement.Height = double.Parse(a.Value);
                            if (a.Name == STYLE_SIZE)
                                nodeElement.Size = double.Parse(a.Value);
                            if (a.Name == STYLE_TYPE)
                                nodeElement.StyleFont = a.Value;
                            if (a.Name == STYLE_WIDTH)
                                nodeElement.With = double.Parse(a.Value);
                            if (a.Name == STYLE_TYPEOFCONTROL)
                                nodeElement.TypeOfControl = a.Value;                           
                        }
                    }                    
                }
                nodeElements.Add(nodeElement);
            }
            return nodeElements;
        }
        
    }

    public enum STYLE_FONT
    {
        REGULAN,BOLD,ILATIC
    }
}
